package com.example.moshudriver.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.BeeFramework.model.BusinessResponse;
import com.BeeFramework.view.EditDialog;
import com.example.moshudriver.adapter.CouponListAdapter;
import com.example.moshudriver.model.DrawModel;
import com.example.moshudriver.model.UserBalanceModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.orderlistpublishedResponse;
import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.IXListViewListener;
import com.external.maxwin.view.XListViews;
import com.sgr.dykdriver.R;

public class CouponActivity extends Activity implements OnClickListener,
		BusinessResponse, IXListViewListener {

	private XListViews couponXList;
	private ImageView mEmptyView,e0_get_img;
	private CouponListAdapter couponAdapter;
	TextView top_view_title;
	EditText recharge_edit;
	ImageView top_view_back;
	UserBalanceModel withdrawModel;
	DrawModel withdraw_list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_coupon);

		initView();
		withdraw_list = new DrawModel(this);
		withdraw_list.addResponseListener(this);
		withdraw_list.getCoupon();
	
	}

	private void initView() {
		e0_get_img= (ImageView) findViewById(R.id.e0_get_img);
	
		e0_get_img.setOnClickListener(this);
		top_view_title = (TextView) findViewById(R.id.top_view_title);
		top_view_title.setText(R.string.my_coupon);
		recharge_edit = (EditText) findViewById(R.id.recharge_edit);
		top_view_back = (ImageView) findViewById(R.id.top_view_back);
		top_view_back.setOnClickListener(this);
		mEmptyView = (ImageView) findViewById(R.id.e0_empty_view);
		mEmptyView.setVisibility(View.GONE);
		
		couponXList = (XListViews) findViewById(R.id.coupon_xlist);
		couponXList
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {

					}
				});
		couponXList.setPullRefreshEnable(true);
		couponXList.setPullLoadEnable(true,0);
		couponXList.setXListViewListener(this, 2);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.e0_get_img:
			//加载布局
			final EditDialog editDialog=new EditDialog(CouponActivity.this, "请输入领取码");
			editDialog.setCancelable(true);
			editDialog.negative.setText("取消");
			editDialog.negative.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					editDialog.dismiss();
				}
			});
			editDialog.positive.setText("确定");
			editDialog.positive.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					editDialog.dismiss();
					//执行领取红包的操作
					if(editDialog.dialog_et_message!=null&&editDialog.dialog_et_message.length()>4){
						withdraw_list.getReciveCoupon(editDialog.dialog_et_message.getText().toString());
					}else{
						Toast.makeText(CouponActivity.this, "请输入正确的号码", 2000).show();
					}
					
					
					
				}
			});
			editDialog.show();
			break;

		case R.id.top_view_back:
			finish();
			break;
		}

	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		couponXList.stopRefresh();
		couponXList.stopLoadMore();
		if (url.endsWith(ApiInterface.WITHDRAW_COUPON)) {

			if (null != jo) {
				couponXList.loadMoreShow();
				mEmptyView.setVisibility(View.GONE);
				couponXList.setVisibility(View.VISIBLE);
				if (null == couponAdapter) {
					couponAdapter = new CouponListAdapter(
							CouponActivity.this, withdraw_list.couponList);
					couponXList.setAdapter(couponAdapter);
				
				} else {
					couponAdapter.notifyDataSetChanged();
				}

				orderlistpublishedResponse response = new orderlistpublishedResponse();
				response.fromJson(jo);
				
				
				if (0 == response.more) {
					couponXList.stopLoadMore();
					couponXList.setPullLoadEnable(false,0);
				} else {
					couponXList.stopLoadMore();
					couponXList.setPullLoadEnable(true,1);
				}
				if(withdraw_list.couponList!=null&&withdraw_list.couponList.size()>0){
					couponXList.setPullLoadEnable(false,1);
				}
				
			} else {
				if (withdraw_list.couponList.size() == 0) {
					mEmptyView.setVisibility(View.VISIBLE);
					couponXList.setVisibility(View.GONE);
				}
			}
		}else if(url.endsWith(ApiInterface.WITHDRAW_RECIVE)){
			Toast.makeText(CouponActivity.this, "领取代金券成功",2000).show();
		}
			

	}

	@Override
	public void onRefresh(int id) {
		withdraw_list.getCoupon();
	}

	@Override
	public void onLoadMore(int id) {
		Toast.makeText(this, "onLoadMore", 1500).show();

	}

}